/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel.layouts;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import org.balisunrise.swing.components.panel.LayoutBuilder;


/**
 * Controí o layout para as ações da entidade.
 * @author Glauco
 */
public class EntityActionLayoutBuilder implements LayoutBuilder{

    private JButton confirmButton;
    private JButton cancelButton;
    private List<JButton> actions = new ArrayList<JButton>();

    public List<JButton> getActions() {
        return actions;
    }

    public void setActions(List<JButton> actions) {
        this.actions = actions;
    }

    public JButton getCancelButton() {
        return cancelButton;
    }

    public void setCancelButton(JButton cancelButton) {
        this.cancelButton = cancelButton;
    }

    public JButton getConfirmButton() {
        return confirmButton;
    }

    public void setConfirmButton(JButton confirmButton) {
        this.confirmButton = confirmButton;
    }

    @Override
    public void buildLayout(JPanel panel) {

        panel.removeAll();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        // Cria grupo Horizontal
        GroupLayout.SequentialGroup sGroup = layout.createSequentialGroup();
        sGroup.addGap(30, 30, Short.MAX_VALUE);
        if (actions.isEmpty()) {
            if (confirmButton != null) {
                sGroup.addComponent(confirmButton);
                if (cancelButton != null) {
                    sGroup.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
                    sGroup.addComponent(cancelButton);
                }
            } else if (cancelButton != null) {
                sGroup.addComponent(cancelButton);
            }
        } else {
            if (confirmButton != null) {
                sGroup.addComponent(confirmButton);
                sGroup.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
            }
            Iterator<JButton> iter = actions.iterator();
            sGroup.addComponent(iter.next());
            while (iter.hasNext()) {
                sGroup.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
                sGroup.addComponent(iter.next());
            }
            if (cancelButton != null) {
                sGroup.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
                sGroup.addComponent(cancelButton);
            }
        }
        sGroup.addGap(30, 30, Short.MAX_VALUE);

        // seta layout horizontal
        layout.setHorizontalGroup(layout.createParallelGroup(
                GroupLayout.Alignment.CENTER).addGroup(sGroup));

        // cria grupo Vertcal
        GroupLayout.ParallelGroup pGroup =
                layout.createParallelGroup(GroupLayout.Alignment.CENTER);
        if (confirmButton != null)
            pGroup.addComponent(confirmButton);
        for (JButton button : actions) {
            pGroup.addComponent(button);
        }
        if(cancelButton != null)
            pGroup.addComponent(cancelButton);

        // seta layout vertical
        layout.setVerticalGroup(pGroup);

    }

}
